//
//  UIBarButtonItem+Extension.swift
//  YYWeibo
//
//  Created by strongwallyi on 15/12/27.
//  Copyright © 2015年 strongwallyi. All rights reserved.
//

import UIKit

extension UIBarButtonItem {

    convenience init(imageName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        
        //初始化一个 button
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //如果有图片就设置图片
        if imageName != nil{
        
            button.setImage(UIImage(named: imageName!), forState: .Normal)
            button.setImage(UIImage(named: "\(imageName!)_highlighted"), forState: .Highlighted)
        }
        
        //如果是文字就设置文字
        if title != nil{
        
            button.setTitle(title, forState: .Normal)
            //设置字体的大小及颜色
            button.titleLabel?.font = UIFont.systemFontOfSize(15)
            button.setTitleColor(UIColor(white: 80/255, alpha: 1), forState: .Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            
        }
        //设置 button 的大小
        button.sizeToFit()
        //将按钮设置成当前类的customView
        customView = button
    }
    
}
